﻿using System;
using Windows.ApplicationModel.DataTransfer;

namespace MVAScratchPad.Services
{
    public class SharingService
    {
        private DataTransferManager DataTransferManager { get; set; }
        public Action<DataPackage> OnShareRequested { get; set; }

        public SharingService()
        {
            DataTransferManager = DataTransferManager.GetForCurrentView();
            DataTransferManager.DataRequested +=
                OnDataTransferManagerDataRequested;
        }

        public void Cleanup()
        {
            DataTransferManager.DataRequested -=
                OnDataTransferManagerDataRequested;
        }

        private void OnDataTransferManagerDataRequested(
            DataTransferManager sender, DataRequestedEventArgs args)
        {
            if (OnShareRequested != null)
            {
                var deferral = args.Request.GetDeferral();
                OnShareRequested(args.Request.Data);
                deferral.Complete();
            }
        }
    }
}